/* 
 * File:   UndoableRedoableCommand.cpp
 * Author: Gab'z
 * 
 * Created on 25 mai 2012, 17:26
 */

#include "UndoableRedoableCommand.h"
#include "../Context.h"

namespace calculator {

    UndoableRedoableCommand::UndoableRedoableCommand(const std::string name) :
    Command(name) {
    }

    UndoableRedoableCommand::~UndoableRedoableCommand() {
    }

    void UndoableRedoableCommand::undo() const throw (CommandException) {
        if (!Context::getInstance()->getMementoCaretaker()->hasPrevious()) throw CommandException("No previous memento");
        try {
            restoreFromMemento(Context::getInstance()->getMementoCaretaker()->getPreviousMemento());
        } catch (MementoException& e) {
            std::string error = "Error while undoing command : ";
            throw CommandException(error + _name);
        }
        // reculer le pointeur des memento
        Context::getInstance()->getMementoCaretaker()->goPrevious();
    }

    void UndoableRedoableCommand::redo() const throw (CommandException) {
        try {
            execute();
        } catch (CommandException& e) {
            std::string error = "Error while redoing command : ";
            throw CommandException(error + _name);
        }
        // avancer le pointeur des memento
        Context::getInstance()->getMementoCaretaker()->goNext();
    }
}
